/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.mgrActionAssemblerGen;

import com.inspur.edp.bef.api.action.assembler.IMgrActionAssembler;
import com.inspur.edp.bef.api.attr.AssemblerMethodAttribute;
import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.bizentity.operation.BizMgrAction;
import com.inspur.edp.bef.component.base.GspComponent;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrMethod;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrMethodParameter;
import com.inspur.edp.bef.dtgenerator.BEGeneratorContext;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.InterfaceMethodGenerateInfo;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.InterfaceMethodGenerator;
import com.inspur.edp.caf.generator.method.InterfaceMethodGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.StringLiteral;

// 自定义mgraction的操作权限如何控制?
public class CustomMgrActionMethodGenerator extends InterfaceMethodGenerator {

  // region Static Members
  public static final String methodNameTemplate = "get%1$sMgrActionAssembler";
  public static final String mgrContextParName = "mgrContext";

  // region Basic Info
  private BEGeneratorContext beGenCtx;
  private BizMgrAction action;
  private BEMgrMethod method;

  public CustomMgrActionMethodGenerator(BEGeneratorContext beGenCtx, BizMgrAction action) {
    this.beGenCtx = beGenCtx;
    this.action = action;
  }

  public static String getMethodName(BizMgrAction mgrAction) {
    return String.format(methodNameTemplate, mgrAction.getCode());
  }

  public static java.util.ArrayList<ParameterInfo> getMethodParameters(
      BEMgrMethod method, BEGeneratorContext beGenCtx) {
    ParameterInfo tempVar = new ParameterInfo();
    tempVar.setParamName(mgrContextParName);
    tempVar.setParamType(
        com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(IBEManagerContext.class));
    java.util.ArrayList<ParameterInfo> rez =
        new java.util.ArrayList<ParameterInfo>(
            java.util.Arrays.asList(tempVar));
    if (method.getParameterCollection() != null) {
      for (Object obj : method.getParameterCollection()) {
        rez.add(
            com.inspur.edp.cef.generator.overall.Utils.buildParGenInfo(
                (BEMgrMethodParameter) obj, beGenCtx, null));
      }
    }
    return rez;
  }

  @Override
  protected InterfaceMethodGeneratorContext createClassInfoContext() {
    return new InterfaceMethodGeneratorContext();
  }

  @Override
  protected InterfaceMethodGenerateInfo createClassMethodInfo() {
    return new InterfaceMethodGenerateInfo();
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>();
  }

  @Override
  protected String getMethodName() {
    return getMethodName(action);
  }

  @Override
  protected void doInitialize() {
    GspMetadata metadata =
        beGenCtx
            .getMetadataPool()
            .getMetadata(action.getComponentPkgName(), action.getComponentId());

    GspComponent comp = (GspComponent) metadata.getContent();
    if (!comp.getComponentType().equals("BEMgrComponent")) {
      throw new RuntimeException("错误的类型");
    }
    method = (BEMgrMethod) comp.getMethod();

    // var compMetadata =
    // (BEMgrComponent)beGenCtx.MetadataPool.getMetadataContent<GspComponentMetadata>(action.getComponentPkgName(), action.getComponentId()).Content;
    // method = compMetadata.BEMgrMethod;
  }

  // endregion

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return getMethodParameters(method, beGenCtx);
  }

  @Override
  protected TypeInfo getReturnType() {
    return Utils.createTypeInfo(IMgrActionAssembler.class);
  }

  @Override
  protected java.util.ArrayList<AnnotationInfo> getAttributeList() {
    java.util.ArrayList<AnnotationInfo> tempVar = new java.util.ArrayList<AnnotationInfo>(1);
    AnnotationInfo info = new AnnotationInfo();
    info.setTypeInfo(
        com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(AssemblerMethodAttribute.class));
    info.setAnnotationType(AnnotationType.SingleMember);
    StringLiteral valueLiteral = ast.newStringLiteral();
    valueLiteral.setLiteralValue(getAssemblerMethodName());
    info.setSingleParam(valueLiteral);
    tempVar.add(info);
    return tempVar;
  }

  private String getAssemblerMethodName() {
    // TODO: 应该是: beCode + "层次" + 动作编号; "层次"暂时先不加;
    return beGenCtx.getBE().getCode() + "" + action.getCode();
  }

  // endregion
}
